#include "wuSimpleMovie.h"

wuSimpleMovie::wuSimpleMovie()
{

}

wuSimpleMovie::~wuSimpleMovie()
{
}

void wuSimpleMovie::setup(string _filename)
{
	movie.loadMovie(_filename);
	movie.play();
	movie.stop();

	movie.setLoopState(OF_LOOP_NONE);
//	movie.idleMovie();
	bPlayMovie = false;
	bCambiazo = false;
	bDragOn = false;
	bPrevisualize = false;
	posSkin.x = 0;
	posSkin.y = 0;
	offset.x = 0;
	offset.y = 0;
}


void wuSimpleMovie::update()
{
	movie.idleMovie();
	if(bPlayMovie)
	{
		int f = movie.getCurrentFrame();
		int t = movie.getTotalNumFrames();
		if (f >= t) {
			movie.stop();
			movie.setFrame(1);
			bPlayMovie=false;
		}//else
//		    movie.idleMovie();
	}

}

void wuSimpleMovie::draw(bool bDrawLines, int offX)
{
	if(bCambiazo || (bPrevisualize && bDrawLines))
	{
		// draw skin
		ofPushMatrix();
			ofEnableAlphaBlending();
			if(bDrawLines && offX==0 && bPrevisualize)
                ofSetColor(255,0,0,150);
			movie.draw(posSkin.x+offX, posSkin.y);
			ofDisableAlphaBlending();
		ofPopMatrix();
	}
}

void wuSimpleMovie::setPosition(ofPoint _pos)
{
	posSkin =_pos;
}




void wuSimpleMovie::dragOff()
{
	bDragOn = false;
}

bool wuSimpleMovie::drag(int _x,int _y)
{
	if(!bDragOn)
	{
		offset.x = _x - posSkin.x;
		offset.y = _y - posSkin.y;
		bDragOn = true;
	}

	posSkin.x = _x -offset.x;
	posSkin.y = _y -offset.y;

    return bDragOn;
}

void wuSimpleMovie::previsualize()
{
    bPrevisualize = true;
    bCambiazo = false;
	movie.stop();
	movie.setFrame(1);
	bPlayMovie=false;
}

void wuSimpleMovie::play()
{
    bPrevisualize = false;
    bCambiazo=true;
	bPlayMovie=true;
	movie.setFrame(1);
	movie.play();
	movie.setLoopState(OF_LOOP_NONE);
}

void wuSimpleMovie::clear()
{
    bCambiazo = false;
	bPlayMovie = false;
	bPrevisualize = false;
	movie.stop();
	movie.setFrame(1);
}
